from django.contrib.auth.models import User
from django.db import models

# Create your models here.
from utils.modelsMixin import *


class Label(DateTimeModelMixin):
    name = models.CharField(max_length=30, verbose_name="标签名", unique=True)

    def __str__(self):
        return self.name


class Article(ModelSetMixin):
    STATUS_CHOICES = (
        (0, "未发布"),
        (1, "发布")
    )
    title = models.CharField(max_length=100, verbose_name="标题")
    digest = models.CharField(max_length=300, verbose_name="摘要")
    content = models.TextField(verbose_name="博客内容")
    page_view = models.IntegerField(default=0, verbose_name="浏览量")
    priority = models.IntegerField(default=0, verbose_name="优先级")
    status = models.IntegerField(verbose_name="状态", default=0, choices=STATUS_CHOICES)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    label = models.ForeignKey(Label, on_delete=models.CASCADE)

    def __str__(self):
        return "标题：%s，作者：%s" % (self.title, self.user)

    class Meta:
        # 排序
        ordering = ["-priority", "-page_view", "-create_time"]


class Comment(ModelSetMixin):
    content = models.TextField(verbose_name="评论的内容")
    article = models.ForeignKey(Article, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    # 外键是可以关联自身 对自身的主键进行关联，字段值可以为空
    # comment_set
    # 父级评论 下面的评论可以有多个
    parent_comment = models.ForeignKey("self", verbose_name="父级评论id", null=True, blank=True, on_delete=models.CASCADE,
                                       # comment_set
                                       related_name="parent_comment_set")
    # 回复哪条评论 被回复的评论下面可以有多个
    reply_comment = models.ForeignKey("self", verbose_name="回复评论", null=True, blank=True, on_delete=models.CASCADE,
                                      # 管理名 关联的表名小写_set comment_set
                                      related_name="reply_comment_set")

    def __str__(self):
        return "%s:%s" % (self.user.username, self.content)

    class Meta:
        ordering = ["-create_time"]
